
import 'package:app/utils/des/dart_des.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/widgets/down_timer_view.dart';
import 'package:flutter_jpf_core/widgets/widget_factory.dart';
import 'package:flutter_jpf_core/widgets/flutter_tree/flutter_tree.dart';
import 'package:flutter_jpf_debug/flutter_jpf_debug.dart';
import 'dart:convert';


class TreeviewPage extends StatefulWidget
{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TreeviewPageState();
  }

}

class TreeviewPageState extends State<TreeviewPage>
{
  List<TreeNodeData> treeData = [];

  TreeNodeData selectData;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    List<Map<String, dynamic>> arr = [
      {
        "title": "陕西省",
        "expaned": true,
        "children": [
          {
            "title": "西安市",
            "expaned": true,
            "children": [ {
              "title": "雁塔区",
              "expaned": true,
              "children": [],
            },{
              "title": "碑林区",
              "expaned": true,
              "children": [],
            },],
          },
        ],
      },
      {
        "title": "山西省",
        "expaned": true,
        "children": [
          {
            "title": "太原市",
            "expaned": true,
            "children": [ {
              "title": "迎泽区",
              "expaned": true,
              "children": [],
            },],
          },
        ],
      }
    ];
    List<TreeNodeData> treeData = [];
    for(int i=0;i<arr.length;i++){
      treeData.add(treeDataFromJson(arr[i],0));
    }
    this.treeData = treeData;
  }

  TreeNodeData treeDataFromJson(json,level){
    return TreeNodeData.fromJson(json,(nodeData){
      List children =json["children"];
      nodeData.level = level;
      nodeData.expaned = json['expaned'] ?? false;
      nodeData.title = json['title'];
      nodeData.leading = json['leading'];
      nodeData.trailing = json['trailing'];
      nodeData.children = children.map((e){return treeDataFromJson(e,level+1);}).toList();

    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: WidgetFatory.createMyAppBar("多级选择", context),
      body:  Container(child: Column(
        children: [
          Container(child:Text(selectData == null? "": selectData.title)),
          TreeView(
            treeData,
            titleOnTap: (data) {
              setState(() {
                if(selectData != null){
                  selectData.isSelect = false;
                }
                selectData = data;
                selectData.isSelect = true;
              });
            },
            leadingOnTap: (TreeNodeData data) {
              setState(() {
                if(selectData != null){
                  selectData.isSelect = false;
                }
                selectData = data;
                selectData.isSelect = true;
              });
            },
            trailingOnTap: (data) {

            },
          )
        ],
      ),)
      ,);
  }

  DownTimerController controller = DownTimerController(totalSeconds: 10);

  void initData(){

  }


}